package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author hutianyi
 * @Date 2020/2/25 11:45
 * @Version v1.0
 * @Description
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Reference
    private RoleService roleService;

    //删除角色数据
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.deleteById(id);
            return new Result(true, MessageConst.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.DELETE_ROLE_FAIL);
        }
    }

    //编辑角色数据
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            //调用service中的更新方法
            roleService.edit(role, permissionIds,menuIds);
            //更新成功, 返回更新成功的信息
            return new Result(true, MessageConst.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //更新失败, 返回更新失败的信息
            return new Result(false, MessageConst.EDIT_ROLE_FAIL);
        }
    }

    //通过角色id权限id
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true,MessageConst.QUERY_PERMISSION_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_PERMISSION_FAIL);
        }
    }

    //通过角色id菜单id
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id){
        try {
            List<Integer> menuIds = roleService.findMenuIdsByRoleId(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }


    //通过角色Id查找角色
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            log.debug("role={}",role.getName(),role.getKeyword(),role.getDescription());
            return new Result(true,MessageConst.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_ROLE_FAIL);
        }

    }


    //新增角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true,MessageConst.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ADD_ROLE_FAIL);
        }
    }

    //角色数据回显
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> allRole = roleService.findAll();
            return new Result(true, "获取所有角色成功", allRole);
        } catch (Exception e) {
            return new Result(true, "获取所有角色失败");
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.debug("QueryPageBean ==={}", queryPageBean);
        try {
            PageResult pageResult = roleService.pageQuery(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L, null);
        }
    }
}
